//
//  CZDoujinDetailViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/19.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZDoujinDetailViewController.h"
#import "SDPhotoBrowser.h"
#import "CZMineViewController.h"
#import "CZNavigationController.h"
@interface CZDoujinDetailViewController () <SDPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UIView *shadowView;
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;
@property (weak, nonatomic) IBOutlet UILabel *rewardLabel;

@property (nonatomic, copy) NSArray *btnArray;
@property (nonatomic, copy) NSArray *photoArr;

@end

@implementation CZDoujinDetailViewController {
    BOOL _success;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _success = NO;
    self.title = @"作品详情";
    [self setTopBtns];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.iconView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImage)]];
    if (![self.dataDic[@"share_pics"] isKindOfClass:[NSNull class]]) {
        self.iconView.yy_imageURL = [NSURL URLWithString:[self.dataDic[@"share_pics"] stringByAppendingString:@"?x-oss-process=style/ios_doujin_50"]];
        self.iconView.userInteractionEnabled = YES;
    }
    if (![self.dataDic[@"author"] isKindOfClass:[NSNull class]]) {
        self.authorLabel.text = self.dataDic[@"author"];
    }
    if (![self.dataDic[@"reward"] isKindOfClass:[NSNull class]]) {
        self.rewardLabel.text = self.dataDic[@"reward"];
    }
    self.shadowView.layer.contentsScale = [UIScreen mainScreen].scale;
    self.shadowView.layer.shadowOpacity = 0.3f;
    self.shadowView.layer.shadowRadius = 4.0f;
    self.shadowView.layer.shadowOffset = CGSizeMake(0,2);
    self.shadowView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.shadowView.bounds].CGPath;
    self.shadowView.layer.masksToBounds = NO;

    // Do any additional setup after loading the view from its nib.
}
- (void)showImage {
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = self.view;
    browser.imageCount = 1;
    browser.delegate = self;
    browser.currentImageIndex = 0;
    browser.indexLabel.hidden = YES;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return nil;
}

-(NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    if (![self.dataDic[@"share_pics"] isKindOfClass:[NSNull class]]) {
        return [NSURL URLWithString:[self.dataDic[@"share_pics"] stringByAppendingString:@"?x-oss-process=style/ios_doujin_50"]];
    }
    return nil;
    
}

- (void)setTopBtns {
    NSArray *titleArray = @[@"打赏5那币", @"打赏20那币", @"打赏233那币"];
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    UIView *backView = [[UIView alloc] init];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.shadowView.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [[UIButton alloc] init];
        [backView addSubview:button];
        [button setTitle:titleArray[i] forState:(UIControlStateNormal)];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [tempArr addObject:button];
        [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        button.layer.cornerRadius = 2;
        button.clipsToBounds = YES;
        button.backgroundColor = mRGBACOLOR(0, 187, 161, (i + 1) / 3.0);
        [button addTarget:self action:@selector(rewardDoujin:) forControlEvents:(UIControlEventTouchUpInside)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        
    }
    
    self.btnArray = tempArr;
//    [self.btnArray mas_distributeViewsAlongAxis:(MASAxisTypeHorizontal) withFixedItemLength:80 leadSpacing:20 tailSpacing:20];
    [self.btnArray mas_distributeViewsAlongAxis:(MASAxisTypeHorizontal) withFixedSpacing:10 leadSpacing:20 tailSpacing:20];
    [self.btnArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backView);
        make.height.equalTo(@40);
    }];
}

- (void)rewardDoujin:(UIButton *)button {
    NSInteger index = [self.btnArray indexOfObject:button];
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/doujin/reward-doujin",APP_HOST];
    
    if (![[NSUserDefaults standardUserDefaults] objectForKey:User_Token]) {
        
        CZBaseTabBarController *rootVC = (CZBaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        CZNavigationController *naviVC = rootVC.childViewControllers[0];
        CZMessionCenterViewController *homeVC = (CZMessionCenterViewController *)naviVC.childViewControllers[0];
        CZNavigationController *mineNC = rootVC.childViewControllers[4];
        CZMineViewController *mineVC = mineNC.childViewControllers[0];
        CZUserLoginViewController * loginVC = [[[CZNsjHelper alloc] init] presentLoginViewControllerWithViewCotroller:self];
        @weakify(mineVC);
        loginVC.callBack = ^(){
            [weak_mineVC getUserData];
            [homeVC setUserIconImage];
            weak_mineVC.userIconImageView.yy_imageURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:User_Logo]];
        };
        
        return;
    }
    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"reward_type" : @(index + 1), @"doujin_id" : self.dataDic[@"doujin_id"] } success:^(id response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            _success = YES;
            NSString *rewardCount = self.rewardLabel.text;
            switch (index) {
                case 0:
                    self.rewardLabel.text = [NSString stringWithFormat:@"%d", rewardCount.integerValue + 5];
                    break;
                case 1:
                    self.rewardLabel.text = [NSString stringWithFormat:@"%d", rewardCount.integerValue + 20];
                    break;
                case 2:
                    self.rewardLabel.text = [NSString stringWithFormat:@"%d", rewardCount.integerValue + 233];
                    break;
                default:
                    break;
            }
            [SVProgressHUD showSuccessWithStatus:response[@"message"]];
        } else if ([response[@"status"] isEqualToString:@"104"]){
            [self presentViewController:[CZNsjHelper createLoginViewController] animated:YES completion:nil];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
            
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated {
    if (self.backBlock) {
        self.backBlock(_success);
    }
    [super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
